//
//  VVSegmentedView.m
//  XLife
//
//  Created by 恩佐John on 16/4/5.
//  Copyright © 2016年 恩佐John. All rights reserved.
//

#import "VVSegmentedView.h"
//#import "UIView+JSExtension.h"
@interface VVSegmentedView ()
@property (nonatomic, strong) NSMutableArray* btnArray;
@property (nonatomic, strong) UIScrollView* scrollView;
@property (nonatomic, strong) UIView* indicateView;
@property (nonatomic, assign) CGFloat btnWidth;     //每个按钮的宽度
@property (nonatomic, assign) NSUInteger divideNum; //self等分的数量



@end

@implementation VVSegmentedView
;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)didMoveToSuperview
{
    [super didMoveToSuperview];
    //底部指示条
    UIButton* btn = [self.btnArray objectAtIndex:0];
    self.indicateView = [[UIView alloc]init];
    self.indicateView.backgroundColor = btn.titleLabel.textColor;
    self.indicateView.My_X = btn.titleLabel.My_X;
    self.indicateView.My_Height = 2;
    self.indicateView.My_Y = self.My_Height - 2;
    self.indicateView.My_Width = btn.titleLabel.My_Width;
    [self.scrollView addSubview:self.indicateView];
    
    
}

//实现初始化方法
- (VVSegmentedView*)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray delegate:(id)delegate
{
    if (self = [super initWithFrame:frame]) {
        //设置self的背景颜色
        self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF" alpha:1];
        //设置代理
        self.delegate = delegate;
        [self setUserInteractionEnabled:YES];
        self.btnArray = [[NSMutableArray alloc]initWithCapacity:[titleArray count]];
        //设置每个按钮的宽度 最多显示4个
        CGFloat btnWidth;
        if (titleArray.count <4) {
            btnWidth = frame.size.width / titleArray.count;
            self.divideNum = titleArray.count;
        }else{
            btnWidth = frame.size.width / 4;
            self.divideNum = 4;
        }
        self.btnWidth = btnWidth;
        
        //设置scrollViewcont
        self.scrollView =[[UIScrollView alloc]initWithFrame:self.bounds];
        self.scrollView.backgroundColor = [UIColor clearColor];
        self.scrollView.userInteractionEnabled = YES;
        self.scrollView.contentSize = CGSizeMake(btnWidth*titleArray.count, frame.size.height);
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.bounces = NO;
        
        [self addSubview:self.scrollView];
        
        for (int i = 0; i < titleArray.count ; i++) {
            UIButton* btn = [[UIButton alloc]init];
//            btn.backgroundColor = [UIColor redColor];
            btn.frame = CGRectMake(i*btnWidth, 0, btnWidth, self.My_Height-2);
            [btn setTitle:titleArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHexString:@"#333333" alpha:1] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHexString:@"#FD851F" alpha:1] forState:UIControlStateSelected];
            //设置btn的tag
            btn.tag = 1000+i;
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.btnArray addObject:btn];
            btn.titleLabel.numberOfLines = 0;
            [btn.titleLabel sizeToFit];
            [btn layoutIfNeeded];
            [self.scrollView addSubview:btn];
            if (i == 0) {
                btn.selected = YES;
            }
//            //添加间隔条
//            if (i<titleArray.count-1) {
//                UIView* view = [[UIView alloc] init];
//                view.frame = CGRectMake(btn.My_Width-1, 5, 1, btn.My_Height-10);
//                view.backgroundColor =[UIColor colorWithHexString:VVBrowseImageViewButtonTitColorNormal alpha:.1];
//                view.backgroundColor = [UIColor redColor];
//                [btn addSubview:view];
//            }
        
        }
    }
    return self;
    
}
- (void)btnClick:(UIButton*)button{
   
    
    NSUInteger index = button.tag -1000;
    
    [self changeVVSegementedViewWithIndex:index isClick:YES];
}


//实现改变index方法
- (void)changeVVSegementedViewWithIndex:(NSInteger)index
{
    
    [self changeVVSegementedViewWithIndex:index isClick:NO];
}


- (void)changeVVSegementedViewWithIndex:(NSInteger)index isClick:(BOOL)isClick{

    //如果超出范围
    if (index > self.btnArray.count-1 || index < 0) {
        return;
    }
    //块遍历
    [self.btnArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton* btn = [_btnArray objectAtIndex:idx];
        if (index != idx) {
            btn.selected = NO;
        }else{
            //动画
            [UIView animateWithDuration:0.25 animations:^{
                
                //选中
                btn.selected = YES;
                self.indicateView.My_X = btn.My_X+btn.titleLabel.My_X;
                self.indicateView.My_Width = btn.titleLabel.My_Width;
                
                
                //btn超出显示范围
                CGPoint pt = CGPointZero;
                BOOL isScroll = NO;
                //当前scrollView的偏移量
                CGFloat contentOffsetX = self.scrollView.contentOffset.x;
                
                //当选择按钮位于self右边时
                if (((index+1)>self.divideNum)&&(contentOffsetX+self.My_Width<self.btnWidth*(index+1))) {
                    pt = CGPointMake(self.btnWidth*(index+1)-(contentOffsetX+self.My_Width), 0);
                    isScroll = YES;
                }
                //当选择按钮是0~diviNum个时,且位于左边
                if (((index+1) <= self.divideNum)&&(contentOffsetX>0)) {
                    pt = CGPointMake(0, 0);
                    isScroll = YES ;
                }
                //当选择按钮位于左边,且index>self.divieNum时
                if (((index+1)>self.divideNum)&&(contentOffsetX>index+1)*self.btnWidth-self.My_Width) {
                    pt = CGPointMake((index+1 - self.divideNum)*self.btnWidth, 0);
                    isScroll = YES;
                }
                
                
                if (isScroll) {
                    [self.scrollView setContentOffset:pt animated:YES];
                }
                
            }];
            
            
        }
    }];
    //响应代理
    if ([self.delegate respondsToSelector:@selector(VVSegementedViewSelectedIndex:isClick:)]) {
        [self.delegate VVSegementedViewSelectedIndex:index isClick:isClick];
    }


}
@end
